<?

/**
 * Este módulo é para ser usado como base para implementação de novos módulos
 *
 */
abstract class Controller implements IController
{	
	/**
	 * Instância do Context
	 *
	 * @var Context
	 */
	protected $_context;
		
	public $uses =  array();
	
	public $usesRoot =  array();
	
	public $comp =  array();
	
	public $compRoot =  array();
	
	public $layout = 'default';
	
	
	/**
	 * Verifica se pode chamar template
	 *
	 * @var boolean
	 */
	public $template = true;

	protected $start;

    protected $end;

	public $cache = false;	
	
	public $data = array();

	/**
	 * UploadFile 
	 *
	 * @var array
	 */
	public $files = array();	


	/**
	* Configuração e inicialização do módulo
	* 
	*@param string $strModuleName
	*@param mixed $customArgs
	*/
	public function Setup()
	{
		$this->start = microtime(true);
		$this->_context = Context::getInstance();
		$this->data = $this->_context->data;
	
		if(count($_FILES))
		{
			foreach ($_FILES as $key => $file)
			{
				/**
				 * UploadFile
				 */
				$this->files[$key] = $this->_context->getUploadedFile($key);	
			}
		}
	}

	/**
	*@return bool
	*@desc requiresAuthentication IController interface.
	*/
	public function requiresAuthentication()
	{
		return false;
	}	

	/**
	*@return bool
	*@desc This module is admin?
	*/
	public function isAdmin()
	{
		return false;
	}

	public function __destruct()
	{
		$this->end = microtime(true);
		$result = $this->end - $this->start;		
		
		/*if ($this->context->getDebug())
		{
			Debug("Tempo de execução do <span style=\"color:red\">módulo</span>: " . $result . ' segundos');
		}
		*/
	}

	public function set($name, $value = '')
	{
		if(is_array($name))
		{
			foreach ($name as  $key => $value)
			{
				$this->_context->addVariavel($key, $value);
			}
		}
		else 
		{
			$this->_context->addVariavel($name, $value);
		}
	}
	
	public function setShow($msn)
	{
		if($msn)
		{
			$this->_context->setSession('myShowF',$msn);
		}		
	}
	
	
	protected function setTemplate($name)
	{
		$this->nameTemplate = $name;
	}
	
	public  function getTemplate()
	{
		return $this->nameTemplate;
	}
	
	public function processNotAutenticated()
	{
		
	}
	
	public function processInsufficientPrivilege ()
	{
		
	}
	
	/**
	 * Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
	 *
	 * @param string $camel_cased_word Camel-cased word to be "underscorized"
	 * @return string Underscore-syntaxed version of the $camel_cased_word
	 * @access public
	 * @static
	 */
	function underscore($camelCasedWord) {
		$replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
		return $replace;
	}
	/**
	 * Returns a human-readable string from $lower_case_and_underscored_word,
	 * by replacing underscores with a space, and by upper-casing the initial characters.
	 *
	 * @param string $lower_case_and_underscored_word String to be made more readable
	 * @return string Human-readable string
	 * @access public
	 * @static
	 */
	function humanize($lowerCaseAndUnderscoredWord) {
		$replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
		return $replace;
	}
	
	
	function createData($dados,$model)
	{
		$this->data[$model->name] = $dados;
	}
	
	protected function render($name, $end)
	{
		if($name)
		{
			$template = new AppTemplate();
			return $template->processRender($name,$end,$this->_context);		
		}		
	
	}

    protected function addControl($control)
    {
         $this->_context->setControl($control);
    }
}
?>